home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!news
- From: jlilley@ix.netcom.com (John Lilley)
- Newsgroups: comp.lang.c++
- Subject: Re: REPOST: Template Class Nested Types
- Date: 27 Mar 1996 05:45:17 GMT
- Organization: Netcom
- Message-ID: <4jakld$d58@dfw-ixnews2.ix.netcom.com>
- References: <4j1c78$1b5@taco.cc.ncsu.edu>
- NNTP-Posting-Host: den-co10-01.ix.netcom.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-NETCOM-Date: Tue Mar 26 11:45:17 PM CST 1996
- X-Newsreader: WinVN 0.99.7
-
- In article <4j1c78$1b5@taco.cc.ncsu.edu>, samebust@pop-in.ncsu.edu says...
- >
- >RE: Template Class Nested Types: Private member functions of the
- > enclosing class that take parameters of the enclosed class type.
- >
- >... cause the compilers (GNU 2.7.0(?) under Linux,
- >Borland C++ 3.1) to choke. They apparently don't recognize the Node
- >class (that is, it appears to be out of scope). But, if I move the body
- >of these member functions into the Binary Tree class declaration (inline),
- >the compilers happily accept the code. I have tried qualifying the
- >Node class with the Tree class name and template parameter list but this
- >didn't help (i.e. tree<type>::node ).
-
- Your code compiles under Borland C++ 4.5, so I slate it as a compiler
- problem. You might try making Node a templated class itself and removing
- it from the tree class:
-
- // SNIPPET 2 //
-
- #include <stddef.h>
-
- template <class info>
- class node
- {
- public:
- info value_;
- node *lchild_, *rchild_;
- };
-
- template <class info>
- class tree
- {
- public:
- tree();
- void Insert(const info &i);
- //
- // other member functions like Delete, etc.
- //
-
- private:
-
- void Insert2(const info &i, node* &np);
- //
- // other private member functions
- //
-
- node<info> *root_;
- };
-
-
- template <class info>
- tree<info>::tree()
- {
- root_=NULL;
- }
-
- template <class info>
- void tree<info>::Insert(const info &i)
- {
- Insert2(i, root_);
- }
-
- template <class info>
- void tree<info>::Insert2(const info &i, node<info>* &np)
- {
- if (np==NULL)
- {
- np=new node;
- // assert (np!=NULL);
- np->lchild_=NULL;
- np->rchild_=NULL;
- np->value_=i;
- }
- else if (i<np->value_)
- Insert2 (i,np->lchild_);
- else if (i>np->value_)
- Insert2 (i,np->rchild_);
- }
-
-
- john lilley
-
-